package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
@Transactional
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    FlavorMapper flavorMapper;
    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        dishMapper.addDish(dish);
        Long dishId = dish.getId();

        //检验是否存在口味，若不存在则不调用mapper
        if (Objects.nonNull(dishDTO.getFlavors()) && !dishDTO.getFlavors().isEmpty()) {
            dishDTO.getFlavors().forEach(flavor -> {
                flavor.setDishId(dishId);
            });
            flavorMapper.addDishFlavours(dishDTO.getFlavors());
        }
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        List<DishVO> dishList;
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        dishList = dishMapper.query(dish);
        Page<DishVO> dishPage = (Page<DishVO>) dishList;
        return new PageResult(dishPage.getTotal(), dishPage.getResult());
    }

    @Override
    public DishVO queryDishById(Long id) {
        //获取Dish对象
        Dish dish;
        dish = dishMapper.queryById(id);
        //获取Flavor对象
        List<DishFlavor> dishFlavors;
        dishFlavors = flavorMapper.queryById(id);
        //构建DishVO并返回
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    @Transactional
    public void deleteAll(List<Long> ids) {
        //检验规则，若已上架无法删除，若在套餐内无法删除
        //1.已上架
        Integer cnt;
        cnt = dishMapper.checkStatus(ids);
        if (cnt != null && cnt > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        //2.在套餐内
        cnt = setmealMapper.checkDishByIds(ids);
        if (cnt != null && cnt > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //核验通过，允许删除
        dishMapper.deleteAll(ids);
        flavorMapper.deleteAll(ids);
    }

    @Override
    public void updateDish(DishDTO dishDTO) {
        //修改dish表中的内容
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        dishMapper.updateDish(dish);
        //修改flavor ，
        //1.将flavor列表中对象加入id字段

        if (Objects.nonNull(dishDTO.getFlavors()) && !dishDTO.getFlavors().isEmpty()) {
            Long id = dish.getId();
            dishDTO.getFlavors().forEach(flavor -> {
                flavor.setDishId(id);
            });
            flavorMapper.deleteAll(Collections.singletonList(dish.getId()));
            flavorMapper.addDishFlavours(dishDTO.getFlavors());
        }
    }

    @Override
    public List queryDishByCategoryId(Integer categoryId) {
        return dishMapper.queryByCategoryId(categoryId);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return DishVO视图对象
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<DishVO> dishList = dishMapper.query(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (DishVO d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = flavorMapper.queryById(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }
        return dishVOList;
    }
}
